Skip to main content

Service Methods Operation/Flows


1. setPreference(dto: CreateNotificationPreferenceDto): Promise of NotificationPreference

Purpose:
Create or update a notification preference for a customer.

Flow:

  • Start
  • Find Customer by dto.customerId
    • If not found → throw NotFoundException("Customer not found")
  • Find NotificationService by dto.notificationServiceId
    • If not found → throw NotFoundException("Notification service not found")
  • Check for existing NotificationPreference by:
    • Customer ID
    • NotificationService ID
    • NotificationType
  • If found:
    • Update status and reason
    • Save and return updated NotificationPreference
  • Else:
    • Create new NotificationPreference
    • Save and return new NotificationPreference

2. createNotificationService(createDto: CreateNotificationServiceDto, userId: number): Promise of NotificationService

Purpose:
Create a new notification service with a unique name, by an admin user.

Flow:

  • Start
  • Check if NotificationService with name.toLowerCase() exists
    • If yes → throw ConflictException("Notification service with name ... already exists")
  • Find Customer by userId with accountTypeId = ADMIN
    • If not found → throw NotFoundException("Admin record not found")
  • Create new NotificationService entity with:
    • name: lowercased
    • createdBy: admin customer
  • Save and return new NotificationService

3. updateNotificationService(id: number, dto: CreateNotificationServiceDto, userId: number): Promise of NotificationService

Purpose:
Update an existing notification service name by an admin, preventing duplicates.

Flow:

  • Start
  • Find NotificationService by id
    • If not found → throw NotFoundException("Notification service not found")
  • Check if another NotificationService with dto.name.toLowerCase() exists and has different id
    • If yes → throw ConflictException("Another notification service with name ... already exists")
  • Find Customer by userId with accountTypeId = ADMIN
    • If not found → throw NotFoundException("Admin record not found")
  • Update:
    • service.name = dto.name.toLowerCase()
    • service.updatedBy = admin customer
  • Save and return updated NotificationService

4. getNotificationService(): Promise of NotificationService array

Purpose:
Retrieve all notification services with createdBy and updatedBy info.

Flow:

  • Start
  • Fetch all NotificationService entities with relations:
    • createdBy (id, firstName, lastName)
    • updatedBy (id, firstName, lastName)
  • If none found → throw NotFoundException("No notification services found")
  • Return list of notification services

5. getEmailNotificationOptOutDetails(token: string): Promise of an object with customer, notificationType, and service

Purpose:
Decode encrypted token to retrieve customer and service details for opt-out.

Flow:

  • Start
  • Decrypt token with secret key → get object with serviceId, customerId, and notificationType
  • Find NotificationService by serviceId
    • If not found → throw NotFoundException("Notification Service not found")
  • Find Customer by customerId
    • If not found → throw NotFoundException("Customer not found")
  • Return object containing customer, notificationType, and service

Purpose:
Generate a secure opt-out URL for customers to disable notifications.

Flow:

  • Start
  • Find NotificationService by serviceId
    • If not found → throw NotFoundException("Service with id ... not found")
  • Encrypt payload with serviceId, customerId, and notificationType using secret key
  • Construct URL: ${webAppDomain}/notification-preferences/opt-out/{encoded token}
  • Shorten link
  • Return object containing the shortened link

Notes

  • All methods validate existence of critical entities (Customer, NotificationService, Admin users).
  • Conflict checks prevent duplicate service names.
  • Encryption/decryption used for secure opt-out link generation.
  • Exceptions are thrown clearly to indicate errors.